package com.pan;

import com.pan.entity.PosNameMapper;
import com.pan.io.WriteCSV;
import com.pan.mapping.LineToObject;
import com.pan.utils.ToStringUtils;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class MyWriteCSV<T> {

    private WriteCSV writeCSV;

    private LineToObject<T> lineToObject;

    public MyWriteCSV(File csvfile, Class<T> objClass, List<PosNameMapper> posNameMapperList){
        this(csvfile,true,objClass,posNameMapperList);
    }

    public MyWriteCSV(File csvfile, boolean append, Class<T> objClass, List<PosNameMapper> posNameMapperList){
        writeCSV = new WriteCSV(csvfile,append);
        lineToObject = new LineToObject<T>(objClass,posNameMapperList);
    }


    private String arrToLineCSV(String[] arr){
        return Arrays.stream(arr).reduce((a, b)->a+","+b).get()+",\n";
    }

    public void write(T object){
        String[] objPropertyArr = lineToObject.objectToArr(object);
        String line = this.arrToLineCSV(objPropertyArr);
        writeCSV.writerLine(line);
    }

    public void writeTitle(String[] titles){
        String inputStr = "";
        for (String s:titles){
            inputStr += titles+",";
        }
        writeCSV.writerLine(inputStr);
    }

    public void close(){
        writeCSV.close();
    }


}
